www.gusucode.com > VC编写的串口调试软件 > VC编写的串口调试软件,内含Modbus协议类 支持对Modbus通讯调试/Modbus串口调试软件1.0/com/RegisteCode.cpp
// RegisteCode.cpp: implementation of the CRegisteCode class. // ////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "RegisteCode.h" #include "Pjsecure.h" #define REG_PERIODOFVALIDITY 30 //注册有效期 #define REG_USEDAYS 366 //注册有效期使用时间 ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// CRegisteCode::CRegisteCode() { m_bRegFlag=false; //第二次注册 m_strRegFile="RegCode.rgc"; m_strRegFlagFile="IODB.exe"; // m_strRegFlagFile="汉字内码.exe"; //* //网卡nic地址 TCHAR pszNic[13]; if (GetNicAddress(pszNic, 13)) { m_strNicAddress=pszNic; TRACE(_T("A network card was detected on your machine with NIC address %s\n"), pszNic); } else { m_strNicAddress="qingfengxiyu"; TRACE(_T("Could not determine the NIC address on this machine\n")); } //硬盘系列号 DWORD dwSerialNumber; if (GetCDriveSerialNumber(&dwSerialNumber)) { m_strDriveSerialNum.Format("%x",dwSerialNumber); TRACE(_T("the serial number of the c drive on your machine is %x\n"), dwSerialNumber); } else { m_strDriveSerialNum="phoenixhao"; TRACE(_T("failed to retreive the serail number\n")); } m_nRegMode=1; //*/ } CRegisteCode::~CRegisteCode() { } //* CString CRegisteCode::GetRegisteInfo() { CString strRegisteCode; char cT[100]; strcpy(cT,m_strDriveSerialNum); int nDSNLen=m_strDriveSerialNum.GetLength(); strRegisteCode=GetORX(m_strDriveSerialNum,"qingfengxiyu"); strRegisteCode+=GetORX(m_strNicAddress,"phoenixhao"); CString strLen; strLen.Format ("%d",nDSNLen); strRegisteCode+=strLen; return strRegisteCode; } //------------------------------------------------------------- //注册 //返回值: // 1-----注册成功 // 0-----注册失败 // -1----已经过期 // -2----系统时间不正确 // -3----已经注册过 // -4----无法保存 //------------------------------------------------------------- int CRegisteCode::Registing(CString strRegisteCode) { // int nSuccess = 0; // if(GetRegFlag()) // return -3; // // if(ReadRegInfo()) // return -3; //已经注册过 CString strHardInfo; strHardInfo=m_strDriveSerialNum; strHardInfo+=m_strNicAddress; int nLen=strHardInfo.GetLength (); char *pucHardInfo=new char[nLen+1]; strcpy(pucHardInfo,strHardInfo); pucHardInfo[nLen]='\0'; unsigned short nRegCode=CRC16((unsigned char*) pucHardInfo,nLen); delete[] pucHardInfo; CString strCRC16,strReg; strCRC16.Format ("%d",nRegCode); strReg=GetORX(strCRC16,"BEIJING"); CString strRegTime,strRegCode; if(strRegisteCode.GetLength ()<16) return 0; strRegTime=strRegisteCode.Mid (0,16); strRegCode=strRegisteCode.Mid (16); strRegTime=ReversionInfo(strRegTime,"Time"); int nYear=atoi(strRegTime.Mid (0,4)); int nMonth=atoi(strRegTime.Mid(4,2)); int nDay=atoi(strRegTime.Mid(6,2)); if(nYear<=0 ||nMonth<=0 ||nDay<=0) //日期不正确 return 0; if(0!=m_nRegMode) { CTime RegTime(nYear,nMonth,nDay,0,0,0); CTime TimeNow=CTime::GetCurrentTime (); TimeNow=CTime(TimeNow.GetYear (),TimeNow.GetMonth (),TimeNow.GetDay (),0,0,0); CTimeSpan TimeSpan=TimeNow-RegTime; TRACE("注册流失时间:%d天\n",TimeSpan.GetDays ()); if(TimeSpan.GetDays ()>REG_PERIODOFVALIDITY ) return -1; //已经过期 if(TimeSpan.GetDays ()<0) return -2; //系统时间不正确 } if(strReg!=strRegCode) return 0; m_strRegCode=strRegisteCode; if(!SaveRegInfo()) return -4; //无法保存 if(!SetRegFlag()) return -5; return 1; } //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ //名称: CRC16------循环容余校验 //---------------------------------------------------- //参数: unsigned char *pucChar-------------接收到的字符 //---------------------------------------------------- //功能: 循环容余校验 //---------------------------------------------------- //返回值: // 校验码 //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ inline unsigned short CRegisteCode::CRC16(const unsigned char *pucChar, unsigned int unLen) { unsigned short usItem=0xA001; unsigned short usCRCReg=0xFFFF; unsigned char ucChar; for(unsigned int i=0;i<unLen;i++) { ucChar=*(pucChar+i); usCRCReg^=ucChar; for(int j=0;j<8;j++) { bool bLSB=((usCRCReg & 0x0001)== 0x0001); usCRCReg=usCRCReg>>1; if(bLSB) { usCRCReg^=usItem; } } } return usCRCReg; } //*/ inline CString CRegisteCode::GetORX(const CString &strSrc, CString strORX) { CString strVal; int nLenSrc=strSrc.GetLength (); int nLenORX=strORX.GetLength (); for(int nIndex=0;nIndex<nLenSrc;nIndex++) { char cSrc=strSrc.GetAt (nIndex); int nORX=nIndex-nIndex/nLenORX*nLenORX; char cORX=strORX.GetAt (nORX); char cVal=cORX^cSrc; //加密时调用 char cHigh=cVal>>4; if(cHigh>=0 && cHigh<=9) cHigh+='0'; else cHigh+='A'; char cLow=cVal & 0x0F; if(cLow>=0 && cLow<=9) cLow+='0'; else cLow+='A'; strVal+=cHigh; strVal+=cLow; // TRACE("--cORX=%c\n",cORX); } return strVal; } inline CString CRegisteCode::Reversion(CString strRegisteCode) { CString strVal; int nLen=strRegisteCode.GetLength (); if(nLen<=0) return strVal; char cDSNLen=strRegisteCode.GetAt (nLen-1); int nDSNLen=atoi(&cDSNLen); CString strDriveSerialNum=strRegisteCode.Mid(0,nDSNLen*2); strDriveSerialNum=ReversionInfo(strDriveSerialNum,"qingfengxiyu"); int nLenNicAddr=nLen-1-nDSNLen*2; CString strNicAddress=strRegisteCode.Mid(nDSNLen*2,nLenNicAddr); strNicAddress=ReversionInfo(strNicAddress,"phoenixhao"); strVal=strDriveSerialNum+strNicAddress; return strVal; } inline CString CRegisteCode::ReversionInfo(CString& strRegInfo,CString strORX) { CString strVal; int nLen=strRegInfo.GetLength (); if(nLen/2*2!=nLen) { strVal="1234567890123456789012345678901234567890"; return strVal; } char* pcVal=new char[nLen/2+1]; pcVal[nLen/2]='\0'; int nIndex; for(nIndex=0;nIndex<nLen;nIndex+=2) { char cHigh=strRegInfo.GetAt (nIndex); char cLow=strRegInfo.GetAt (nIndex+1); if(cHigh>='0' && cHigh<='9') cHigh-='0'; else cHigh-='A'; if(cLow>='0' && cLow<='9') cLow-='0'; else cLow-='A'; pcVal[nIndex/2]=cHigh<<4 | cLow; } // int nLenORX=strORX.GetLength (); for(nIndex=0;nIndex<nLen/2;nIndex++) { char cSrc=pcVal[nIndex]; int nORX=nIndex-nIndex/nLenORX*nLenORX; char cORX=strORX.GetAt (nORX); char cVal=cORX^cSrc; strVal+=cVal; } delete[] pcVal; return strVal; } CString CRegisteCode::GetRegisteCode(const CString &strRegInfo) { CString strHardInfo=Reversion(strRegInfo); int nLen=strHardInfo.GetLength (); char *pucHardInfo=new char[nLen+1]; strcpy(pucHardInfo,strHardInfo); pucHardInfo[nLen]='\0'; unsigned short nRegCode=CRC16((unsigned char*) pucHardInfo,nLen); delete[] pucHardInfo; CString strCRC16,strRegCode; strCRC16.Format ("%d",nRegCode); strRegCode=GetORX(strCRC16,"BEIJING"); //注册时间 GetRegTime(); CString strRegTime=GetORX(m_strRegTime,"Time"); strRegCode=strRegTime+strRegCode; return strRegCode; } bool CRegisteCode::SaveRegInfo() { CString strFile; strFile=GetExeFullPath()+m_strRegFile; CFile f; CFileException e; if( !f.Open( strFile, CFile::modeCreate | CFile::modeWrite, &e ) ) { #ifdef _DEBUG afxDump << "File could not be opened " << e.m_cause << "\n"; #endif return false; } //注册码和正式注册时间 CTime TimeNow=CTime::GetCurrentTime (); TimeNow=CTime(TimeNow.GetYear (),TimeNow.GetMonth (),TimeNow.GetDay (),0,0,0); CString strTime=TimeNow.Format ("%Y%m%d"); CString strTimePW=GetORX(strTime,"china"); f.Write(strTimePW,strTimePW.GetLength ()); f.Write (m_strRegCode,m_strRegCode.GetLength ()); f.Close (); return TRUE; } bool CRegisteCode::ReadRegInfo() { CString strFile; strFile=GetExeFullPath()+m_strRegFile; CFile f; if( !f.Open(strFile, CFile::modeRead ) ) { return FALSE; } //读入注册信息 DWORD dwEnd =f.SeekToEnd (); char* pcRegCode=new char[dwEnd+1]; pcRegCode[dwEnd]='\0'; f.SeekToBegin(); f.Read (pcRegCode,dwEnd); f.Close (); m_strRegCode=pcRegCode; delete[] pcRegCode; return true; } void CRegisteCode::GetRegTime() { CTime Time= CTime::GetCurrentTime (); int nYear=Time.GetYear (); int nMonth=Time.GetMonth (); int nDay=Time.GetDay (); m_strRegTime=Time.Format ("%Y%m%d"); } bool CRegisteCode::SetRegFlag() { return true; CString strFile; strFile=GetExeFullPath()+m_strRegFlagFile; CFile f; CFileException e; if( !f.Open( strFile,CFile::modeWrite, &e ) ) { #ifdef _DEBUG afxDump << "File could not be opened " << e.m_cause << "\n"; if(CFileException::sharingViolation==e.m_cause) int i=0; #endif return false; } DWORD dwActual =f.Seek (-8,CFile::end); if(!m_bRegFlag) //表示没有注册 f.SeekToEnd (); CTime TimeNow=CTime::GetCurrentTime (); CString strTime=TimeNow.Format ("%Y%m%d"); char cNowTime[9]; strcpy(cNowTime,strTime); f.Write (&cNowTime,8); f.Close (); return true; } bool CRegisteCode::GetRegFlag() { return true; CString strFile; strFile=GetExeFullPath()+m_strRegFlagFile; CFile f; CFileException e; if( !f.Open( strFile, CFile::modeRead, &e ) ) { #ifdef _DEBUG afxDump << "File could not be opened " << e.m_cause << "\n"; #endif return false; } DWORD dwActual =f.Seek (-8,CFile::end); char cNowTime[9]; cNowTime[8]='\0'; f.Read (&cNowTime,8); f.Close (); if(cNowTime[5]==0 && cNowTime[6]==0 && cNowTime[7]==0 ) //表示没有注册 m_bRegFlag=false; else m_bRegFlag=true; int nDate=atoi(cNowTime); int nYear=nDate/10000; nDate-=nYear*10000; int nMonth=nDate/100; int nDay=nDate-nMonth*100; if(nYear==0 ||nMonth==0 ||nDay==0) return false; try{ m_LastAccessTime=CTime(nYear,nMonth,nDay,0,0,0); } catch(...) { TRACE("CRegisteCode::GetRegFlag()--注册时间不正确\n"); return false; } return true; } CString CRegisteCode::GetExeFullPath() { CString strPath; TCHAR ExeFullPath[MAX_PATH]; if(:: GetModuleFileName( NULL, //HMODULE hModule, // handle to the module ExeFullPath, //LPTSTR lpFilename, // buffer that receives the path MAX_PATH //DWORD nSize // size of the buffer )) { strPath=ExeFullPath; int nStart=0; int nFind=strPath.Find ("\\",nStart); while(nFind>0) { nStart=nFind+1; nFind=strPath.Find ("\\",nStart); } strPath=strPath.Mid (0,nStart); } return strPath; } int CRegisteCode::IsRegisted() { bool bRegFlag=GetRegFlag(); if(!ReadRegInfo() || !bRegFlag) return 0; CString strHardInfo; strHardInfo=m_strDriveSerialNum; strHardInfo+=m_strNicAddress; int nLen=strHardInfo.GetLength (); char *pucHardInfo=new char[nLen+1]; strcpy(pucHardInfo,strHardInfo); pucHardInfo[nLen]='\0'; unsigned short nRegCode=CRC16((unsigned char*) pucHardInfo,nLen); delete[] pucHardInfo; CString strCRC16,strReg; strCRC16.Format ("%d",nRegCode); strReg=GetORX(strCRC16,"BEIJING"); CString strRegTime,strRegPublishTime,strRegCode; if(m_strRegCode.GetLength ()<32) return 0; strRegTime=m_strRegCode.Mid (0,16); strRegPublishTime= m_strRegCode.Mid (16,16); strRegCode=m_strRegCode.Mid (32); if(strReg!=strRegCode) return -3; if(0==m_nRegMode) return true; strRegPublishTime= ReversionInfo(strRegPublishTime,"Time"); strRegTime=ReversionInfo(strRegTime,"china"); int nYear=atoi(strRegTime.Mid (0,4)); int nMonth=atoi(strRegTime.Mid(4,2)); int nDay=atoi(strRegTime.Mid(6,2)); if(nYear<=0 ||nMonth<=0 ||nDay<=0) //日期不正确 return -3; int nPubYear=atoi(strRegTime.Mid (0,4)); int nPubMonth=atoi(strRegTime.Mid(4,2)); int nPubDay=atoi(strRegTime.Mid(6,2)); if(nPubYear<=0 ||nPubMonth<=0 ||nPubDay<=0) //日期不正确 return -3; CTime RegTime(nYear,nMonth,nDay,0,0,0); CTime PubTime(nPubYear,nPubMonth,nPubDay,0,0,0); CTime TimeNow=CTime::GetCurrentTime (); TimeNow=CTime(TimeNow.GetYear (),TimeNow.GetMonth (),TimeNow.GetDay (),0,0,0); CTimeSpan RegTimeSpan=TimeNow-RegTime; CTimeSpan PubTimeSpan=TimeNow-PubTime; TRACE("距注册时间:%d天\n",RegTimeSpan.GetDays ()); /* CString strFileName=GetExeFullPath()+m_strRegFile; CTime CreateTime; CFileFind FindFile; FindFile.FindFile (strFileName); FindFile.FindNextFile (); // TRACE("CFindFile::FindNextFile()---%s\n",FindFile.GetFileName ()); FindFile.GetCreationTime (CreateTime); //*/ CTimeSpan CreateTimeSpan=TimeNow-m_LastAccessTime; if(RegTimeSpan.GetDays ()>REG_USEDAYS || PubTimeSpan.GetDays ()>REG_USEDAYS +REG_PERIODOFVALIDITY) return -1; //已经过期 if(RegTimeSpan.GetDays ()<0 || PubTimeSpan.GetDays ()<0 || CreateTimeSpan.GetDays ()<0 ) return -2; //系统时间不正确 return 1; } int CRegisteCode::VerifyRegCode(CString strRegisteCode) { CString strHardInfo; strHardInfo=m_strDriveSerialNum; strHardInfo+=m_strNicAddress; int nLen=strHardInfo.GetLength (); char *pucHardInfo=new char[nLen+1]; strcpy(pucHardInfo,strHardInfo); pucHardInfo[nLen]='\0'; unsigned short nRegCode=CRC16((unsigned char*) pucHardInfo,nLen); delete[] pucHardInfo; CString strCRC16,strReg; strCRC16.Format ("%d",nRegCode); strReg=GetORX(strCRC16,"BEIJING"); CString strRegTime,strRegCode; if(strRegisteCode.GetLength ()<12) return 0; strRegTime=strRegisteCode.Mid (0,12); strRegCode=strRegisteCode.Mid (12); strRegTime=ReversionInfo(strRegTime,"Time"); int nYear=atoi(strRegTime.Mid (0,2)); int nMonth=atoi(strRegTime.Mid(2,2)); int nDay=atoi(strRegTime.Mid(4,2)); if(nYear<=0 ||nMonth<=0 ||nDay<=0) //日期不正确 return 0; if(0!=m_nRegMode) { //与时间无关 nYear+=2000; CTime RegTime(nYear,nMonth,nDay,0,0,0); CTime TimeNow=CTime::GetCurrentTime (); TimeNow=CTime(TimeNow.GetYear (),TimeNow.GetMonth (),TimeNow.GetDay (),0,0,0); CTimeSpan TimeSpan=TimeNow-RegTime; TRACE("注册流失时间:%d天\n",TimeSpan.GetDays ()); if(TimeSpan.GetDays ()>REG_PERIODOFVALIDITY ) return -1; //已经过期 if(TimeSpan.GetDays ()<0) return -2; //系统时间不正确 } if(strReg!=strRegCode) return 0; return true; } CString CRegisteCode::GetRegisteCode() { if(m_strRegCode.IsEmpty ()) ReadRegInfo(); return m_strRegCode; } void CRegisteCode::SetAccessFlag() { SetRegFlag(); } int CRegisteCode::SetRegMode(int nRegMode) { int nOld=m_nRegMode; m_nRegMode=nRegMode; return m_nRegMode; }